﻿using System;
using System.Globalization;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace SGLLv2.UI
{
    class NumericUpDownWatermarked : NumericUpDown
    {
        // Declare Global Variables
        private readonly TransparentLabel label;

        #region Properties
        /// <summary>
        /// Text to appear on the Text Box
        /// </summary>
        [Browsable(true)]
        public string WatermarkText
        {
            get
            {
                return label.Text;
            }
            set
            {
                label.Text = value;
            }
        }
        /// <summary>
        /// Color of the text to appear on the Text Box
        /// </summary>
        [Browsable(true)]
        public Color WatermarkColor
        {
            get
            {
                return label.ForeColor;
            }
            set
            {
                label.ForeColor = value;
            }
        }
        /// <summary>
        /// Font Type of the Text to appear on the Text Box
        /// </summary>
        [Browsable(true)]
        public Font WatermarkFont
        {
            get
            {
                return label.Font;
            }
            set
            {
                label.Font = value;
            }
        }
        #endregion
        
        /// <summary>
        /// Class Constructor
        /// Initialize a new instance of this class
        /// </summary>
        public NumericUpDownWatermarked()
        {
            label = new TransparentLabel();
            label.Click += label_Click;
            label.TextAlign = ContentAlignment.MiddleLeft;
            label.Dock = DockStyle.Fill;
            Controls.Add(label);
            Controls.SetChildIndex(label, 1);
        }

        #region Events
        void label_Click(object sender, EventArgs e)
        {
            OnGotFocus(e);
            Focus();
        }
        protected override void OnClick(EventArgs e)
        {
            label.Visible = false;
            Select(0, Maximum.ToString(CultureInfo.InvariantCulture).Length);
            base.OnClick(e);
        }
        protected override void OnValueChanged(EventArgs e)
        {
            label.Visible = Value.Equals(Minimum);
            base.OnValueChanged(e);
        }
        protected override void OnGotFocus(EventArgs e)
        {
            label.Visible = false;
            Select(0, Maximum.ToString(CultureInfo.InvariantCulture).Length);
            base.OnGotFocus(e);
        }
        protected override void OnLostFocus(EventArgs e)
        {
            label.Visible = Value.Equals(Minimum) || String.IsNullOrWhiteSpace(base.Text);
            if (String.IsNullOrWhiteSpace(base.Text)) Value = Minimum;
            base.OnLostFocus(e);
        }
        #endregion
    }
}